//form监听Tree操作事件
layui.use(['form','jquery','tree'],function (){
    var form = layui.form;
    var $ = layui.$;
    var tree = layui.tree;

    //监听添加模板的提交
    form.on('submit(addtreenode)',function (d){
        $.ajax({
            type:'post',
            url:'/addClassesTree',
            data:d.field,
            dataType:'json',
            success:function (res){
                if (res.state == '200'){
                    layer.msg('添加成功',{time:500,icon:6},function (){
                        //弹框的关闭
                        layer.close(layindex);
                        //刷新树
                        getData();
                    })
                }else{
                    layer.msg("添加节点失败",{time: 1000,icon:5},function (){
                        //先得到当前iframe层的索引
                        let index = parent.layer.getFrameIndex(window.name);
                        //再执行关闭
                        parent.layer.close(index);

                    })
                }
            },
            error:function (e){
                console.log(e)
            }
        })
        return false;
    })

    //监听修改模板的提交
    form.on('submit(uptreenode)',function (d){
        $.ajax({
            type:'post',
            url:'/upClassesTree',
            data:d.field,
            dataType:'json',
            success:function (res){
                if (res.state == '200'){
                    layer.msg('修改成功',{time:500,icon:6},function (){
                        //弹框的关闭
                        layer.close(layindex);
                        //刷新树
                        getData();
                    })
                }else{
                    layer.msg("修改节点失败",{time: 1000,icon:5},function (){
                        //先得到当前iframe层的索引
                        let index = parent.layer.getFrameIndex(window.name);
                        //再执行关闭
                        parent.layer.close(index);

                    })
                }
            },
            error:function (e){
                console.log(e)
            }
        })
        return false;
    })

    //监听顶部Tree添加按钮
    var OneIndex = null;
    form.on('submit(tjOneTree)',function (d){
        OneIndex = layer.open({
            type:1,
            title:'添加一级系目录',
            content:$('#addOneTree'),
            area:['60%','60%']
        })
    })
    //弹出的一级Tree监听
    form.on('submit(addOneTree)',function (d){

        $.ajax({
            type:'post',
            url:'/addClassesTree',
            data:d.field,
            dataType:'json',
            success:function (res){
                if (res.state == '200'){
                    layer.msg('添加成功',{time:500,icon:6},function (){
                        //弹框的关闭
                        layer.close(OneIndex);
                        //刷新树
                        getData();
                    })
                }else{
                    layer.msg("添加一级节点失败",{time: 1000,icon:5},function (){
                        //先得到当前iframe层的索引
                        let index = parent.layer.getFrameIndex(window.name);
                        //再执行关闭
                        parent.layer.close(index);

                    })
                }
            },
            error:function (e){
                console.log(e)
            }
        })

    })

})

